<?php

	/**
	*		SilverMapper v1.0.0
	*		@lastmodified: November 10th, 2008
	*		@author: Michael Giddens
	*		@company: SilverBiology
	*		@website: www.SilverBiology.com
	*
	*		This file is part of SilverMapper.
	*		http://code.google.com/p/silvermapper/
	*
	*		SilverMapper is free software: you can redistribute it and/or modify
	*		it under the terms of the GNU General Public License as published by
	*		the Free Software Foundation, either version 3 of the License, or
	*		(at your option) any later version.
	*	
	*		SilverMapper is distributed in the hope that it will be useful,
	*		but WITHOUT ANY WARRANTY; without even the implied warranty of
	*		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	*		GNU General Public License for more details.
	*	
	*		You should have received a copy of the GNU General Public License
	*		along with SilverMapper.  If not, see <http://www.gnu.org/licenses/>.
	*/

//	error_reporting(E_ALL);
	ini_set("display_errors", 1);
 	
	define( 'VERSION', '1.0.0' );
	define( 'SOFTWARE', 'SilverMapper' );
	define( 'PROJECT', 'http://code.google.com/p/silvermapper/' );
	
	require_once('../config.php');
	require_once('class.misc.php');
	require_once('class.wainvasive-introduced.php');
	require_once('class.wainvasive-native.php');
	require_once('class.classcweeds.php');
	
	switch( $_REQUEST['task'] ) {

		case 'ping':
			print_c( json_encode( array( 'success' => true, 'software' => SOFTWARE, 'version' => VERSION, 'project' => PROJECT ) ) );
			break;


		case 'list':

			$wainvasive = new WainvasiveIntroduced();
			$records = $wainvasive->getList( $_REQUEST['type'], $_REQUEST['filter'] );

			print_c( json_encode( array( 'nodes' => $records ) ) );
			break;

		/**
		*		This case will return the record count per year between earliest record and most recent record.
		*
		* 	return( json )
		*			success: bool
		*			records: array( Date, Layer1, Layer2, {...} )
		**/
		case 'recordsByYear':
		
			function firstYear( $a ) {
				$earliest = 2050;
				foreach( $a as $year ) {
					if ($year != NULL) {
						$lastYear = split('-', $year);
						if ($lastYear[0] < $earliest) {
							$earliest = $lastYear[0];
						}
					}
				}
				return( $earliest );
			}

			function lastYear( $a ) {
				$earliest = 0;
				foreach( $a as $year ) {
					if ($year != NULL) {
						$lastYear = split('-', $year);
						if ($lastYear[0] > $earliest) {
							$earliest = $lastYear[0];
						}
					}
				}
				return( $earliest );
			}

			$layers = json_decode( $_REQUEST['layers'], true );

			if ( count($layers) ) {
				foreach( $layers as $value ) {
					switch ($value) {
						case 0:
							$wainvasive = new WainvasiveIntroduced();
							$records_0 = $wainvasive->getRecordsByYear( $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'] );
							break;
	
						case 1:
							$cweeds = new CWeeds();
							$records_1 = $cweeds->getRecordsByYear( $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'], $_REQUEST['filters'] );
							break;
	
						case 2:
							$wainvasive = new WainvasiveNative();
							$records_2 = $wainvasive->getRecordsByYear( $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'] );
							break;
							
					}
				}
			}				
			if ( !isset( $records_0 ) ) {
				$records_0[0]->name = 'Layer0';
			}

			if ( !isset( $records_1 ) ) {
				$records_1[0]->name = 'Layer1';
			}

			if ( !isset( $records_2 ) ) {
				$records_2[0]->name = 'Layer2';
			}
			
			$firstYear = firstYear( array( @$records_0[0]->Date, @$records_1[0]->Date, @$records_2[0]->Date ) );
			$lastYear = lastYear( array( @$records_0[ count($records_0)-1 ]->Date, @$records_1[ count($records_1)-1 ]->Date, @$records_2[ count($records_2)-1 ]->Date ) );

			$tmpRecords = array();

			if ($firstYear != 0 && $lastYear != 2050) {
			
				function getYear( $year, $records ) {
	
					foreach( $records as $record ) {
	
						$tmpYear = split('-', $record[0]->Date);
						$tmpYear = $tmpYear[0];
						if ($tmpYear == $year) {
							print $record[0]->sz . "<br>";
							$tmpRecord[ $record[0]->name ] = $record[0]->sz;
							array_shift($record);
						} else {
							$tmpRecord[ $record[0]->name ] = 0;
						}
						
					}
					
					$tmpRecord['Date'] = $year;
					return( $tmpRecord );
				}
				
				for ( $year = $firstYear; $year <= $lastYear; $year++ ) {
					$records = array( &$records_0, &$records_1, &$records_2 );
//print "<pre>";					
//	print_r($records);
					foreach( $records as $index => &$record ) {
//print $index;					
						if ($record == NULL) {
							$tmpRecord[ 'Layer' . $index ] = 0;
						} else {
							$tmpYear = @split('-', $record[0]->Date);
							$tmpYear = $tmpYear[0];
							if ($tmpYear == $year) {
//								$tmpRecord[ @$record[0]->name ] = @$record[0]->sz;
								$tmpRecord[ 'Layer' . $index ] = @$record[0]->sz;
								array_shift($record);
							} else {
								$tmpRecord[ @$record[0]->name ] = 0;
							}
						}
					}
					
					$tmpRecord['Date'] = $year . "-01-01";
					
					$tmpRecords[] = $tmpRecord;
					
				}
			}
						
			print_c( json_encode( array( 'success' => true, 'records' => $tmpRecords ) ) );
			break;
			
		/**
		*		This case will return the records for a particular gepspatial point.
		*
		* 	return( json )
		*			success: bool
		*			totalCount: int (Size of total number of records in the db that match condition)
		*			markers: array( id, Family, Genus, SpecificEpithet, DecimalLatitude, DecimalLongitude, html_summary, html_details
		**/
		case 'recordsByPoint':
		
			switch( @$_REQUEST['layer'] ) {
			
				case 0:
					$wainvasive = new WainvasiveIntroduced();
					$tmp = $wainvasive->getRecordsByPoint( $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'], $_REQUEST['DecimalPercision'], $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'] );
					break;
					
				case 1:
					$cweeds = new CWeeds();
					$tmp = $cweeds->getRecordsByPoint( $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'], $_REQUEST['DecimalPercision'], $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'] );
					break;

				case 2:
					$wainvasive = new WainvasiveNative();
					$tmp = $wainvasive->getRecordsByPoint( $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'], $_REQUEST['DecimalPercision'], $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'] );
					break;
					
			}

			print_c( json_encode( array( 'success' => true, 'totalCount' => $tmp['totalCount'], 'markers' => $tmp['records'] ) ) );
			break;

		/**
		*		This case will return the records for a particular gepspatial point.
		*
		* 	return( json )
		*			success: bool
		*			totalCount: int (Size of total number of records in the db that match condition)
		*			markers: array( id, Family, Genus, SpecificEpithet, DecimalLatitude, DecimalLongitude, html_summary, html_details
		**/
		case 'recordsByRegion': 

			$zoom = $_REQUEST['zoom'];
			
			if ($zoom < 5) $zoom = 5;
			
			$dp =  (int) ( ($zoom - 5) / 2 );
			
			if ($dp >= 0) {
				$dp--;

				$layers = json_decode( $_REQUEST['layers'], true );

				$records = array();
				
				foreach( $layers as $layer ) {
					switch( $layer ) {
					
						case 0:
							$wainvasive = new WainvasiveIntroduced();
							$records = array_merge( $records, $wainvasive->getRecordsByRegion( $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'], $dp ) );
							break;
							
						case 1:
							$cweeds = new CWeeds();
							$records = array_merge( $records, $cweeds->getRecordsByRegion( $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'], $dp ) );
							break;

						case 2:
							$wainvasive = new WainvasiveNative();
							$records = array_merge( $records, $wainvasive->getRecordsByRegion( $_REQUEST['sw_lat'], $_REQUEST['ne_lat'], $_REQUEST['sw_lng'], $_REQUEST['ne_lng'], $dp ) );
							break;
							
					}
				}

				if ($records == null) {
					$records = array();
				}

				print_c( json_encode( array( 'success' => true, 'markers' => $records ) ) );
				
			} else {
				print_c( json_encode( array( 'success' => false ) ) );
			}
			break;

	}

	/**
	*		Function print_c (Print Callback)
	*		This is a wrapper function for print that will place the callback around the output statement
	**/
	function print_c( $str ) {
		if ( isset( $_REQUEST['callback'] ) ) {
			$cb = $_REQUEST['callback'] . '(' . $str . ')';
		} else {
			$cb = $str;
		}
		
		print $cb;
	}
	
?>